/**
 * @Author :sunwei
 * @Description:
 * @Date create in 21:22  2021/5/23
 * @Modified by:
 */
public class SingleThreadVersion {
    private final String LOCK = "LOCK";
    private int index = 0;

    //true : 生产了 ,false:消费了
    private volatile boolean isProduce = false;

    public void produce() {
        synchronized (LOCK) {
            if (isProduce) {
                try {
                    System.out.println("wait consume... " );
                    //等待消费
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("p-->" + ++index);
                isProduce = !isProduce;
                //通知再消费
                LOCK.notify();

            }
        }
    }

    public void consume() {
        synchronized (LOCK) {
            if (isProduce) {
                System.out.println("c-->" + index);
                isProduce = !isProduce;
                //通知再生产
                LOCK.notify();

            } else {
                try {
                    System.out.println("wait produce... " );
                    //等待生产
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
